//
//  AppDatabase.swift
//  GRDBDemo
//
//  Created by Rdxer on 2017/12/23.
//  Copyright © 2017年 Rdxer. All rights reserved.
//

import UIKit
import GRDB


let mainDbFileName = "maim.db"

/// AppDatabase.setupMainDatabase(application)
class AppDB {
    
    static var main: DatabaseQueue!
    
    ///init
    static func setupMainDatabase(_ application: UIApplication){
        do {
            let databaseURL = try FileManager.default
                .url(for: .applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
                .appendingPathComponent(mainDbFileName)
            print("[DBPath] >> " + databaseURL.path)
            main = try GRDBEx.openDatabase(atPath: databaseURL.path, migrator: mainDatabaseMigratorConfig())
            main.setupMemoryManagement(in: application)
        } catch  {
            assert(false, "初始化数据库失败\(error.localizedDescription)")
        }
    }
}
